package com.hcxy.filetransform.util;

import com.hcxy.filetransform.util.Handler.Message;

public abstract class AsyncTask<Params,Progress,Result> {
	private ProgressHandler progressHandler = new ProgressHandler();
	private static final int MSG_PROGRESS_UPDATE = 0x001;
	protected abstract Result doInBackground(Params... params);
	protected  void onProgressUpdate(Progress progress){
		
	}
	
	public void publisProgrss(Progress progress){
		Message msg = progressHandler.new Message(MSG_PROGRESS_UPDATE,progress);
		progressHandler.sendMessage(msg);
	}
	public final void execute(final Params... params){
		new Thread(new Runnable() {
			@Override
			public void run() {
				doInBackground(params);
			}
		}).start();
	}
	private class ProgressHandler extends Handler{
		@Override
		protected void handlerMessage(Message msg) {
			if (msg.what==MSG_PROGRESS_UPDATE) {
				onProgressUpdate((Progress) msg.obj1);
			}
		}
		
	}
}